package com.xuanheng.utils;

import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.clj.fastble.conn.BleCharacterCallback;
import com.clj.fastble.data.ScanResult;
import com.clj.fastble.exception.BleException;
import com.xuanheng.app.App;

import java.util.List;

/**
 * Created by liyunhan on 2017/11/4.
 */

public class BleScaleUtil {
    private BluetoothService mBluetoothService;
    private static final BleScaleUtil ourInstance = new BleScaleUtil();

    private String deviceName;
    private String serviceName;
    private String charactName;

    private BleScaleListener bleScaleListener;
    public static BleScaleUtil getInstance() {
        return ourInstance;
    }

    private BleScaleUtil() {
        Intent bindIntent = new Intent(App.getContext(), BluetoothService.class);
        App.getContext().bindService(bindIntent, mFhrSCon, Context.BIND_AUTO_CREATE);
    }


    public void onDestroy() {
        App.getContext().unbindService(mFhrSCon);
    }

    public void startScan(String deviceName, String serviceName, String charactName) {
        this.deviceName = deviceName;
        this.serviceName = serviceName;
        this.charactName = charactName;
        if (mBluetoothService != null) {
            mBluetoothService.scanAndConnect2(deviceName);
        }
    }

    public void setBleScaleListener(BleScaleListener bleScaleListener) {
        this.bleScaleListener = bleScaleListener;
    }

    private ServiceConnection mFhrSCon = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mBluetoothService = ((BluetoothService.BluetoothBinder) service).getService();
            mBluetoothService.setScanCallback(callback);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            mBluetoothService = null;
        }
    };

    private BluetoothService.Callback callback = new BluetoothService.Callback() {
        @Override
        public void onStartScan() {

        }

        @Override
        public void onScanning(ScanResult result) {
            bleScaleListener.onConnectting();
        }

        @Override
        public void onScanComplete() {

        }

        @Override
        public void onConnecting() {
bleScaleListener.onConnectting();
        }

        @Override
        public void onConnectFail() {
bleScaleListener.onConnectFail();
        }

        @Override
        public void onDisConnected() {
bleScaleListener.onDisconnect();
        }

        @Override
        public void onServicesDiscovered() {
            bleScaleListener.onConnect();
            try {
                List<BluetoothGattService> gattServices = mBluetoothService.getGatt().getServices();
                for (BluetoothGattService service : gattServices) {
                    if (service.getUuid().toString().contains(serviceName)) {
                        mBluetoothService.setService(service);
                        for (BluetoothGattCharacteristic characteristic : service.getCharacteristics()) {
                            if (characteristic.getUuid().toString().contains(charactName)) {
                                mBluetoothService.setCharacteristic(characteristic);
                                mBluetoothService.notify(
                                        characteristic.getService().getUuid().toString(),
                                        characteristic.getUuid().toString(), bleCharacterCallback);
                            }
                        }
                    }
                }
            } catch (Exception e) {
                bleScaleListener.onReciveError(e.getMessage());
            }

        }
    };

    private BleCharacterCallback bleCharacterCallback = new BleCharacterCallback() {
        @Override
        public void onSuccess(BluetoothGattCharacteristic characteristic) {

            bleScaleListener.onReciveData(characteristic.getValue());
        }

        @Override
        public void onFailure(BleException e) {
            bleScaleListener.onReciveError(e.getDescription());
        }

        @Override
        public void onInitiatedResult(boolean result) {
        }
    };
}
